/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package fr.DRHclient.servlet;

import fr.DRHclient.Consts;
import fr.DRHclient.models.ProfileViewModel;
import fr.annuaireComp.entities.Personnel;
import fr.annuaireComp.sessions.PersonnelManager;
import java.io.IOException;
import java.text.SimpleDateFormat;
import javax.ejb.EJB;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import fr.DRHclient.utils.AuthManager;
import fr.annuaireComp.clientUtils.CompetenceModel;
import fr.annuaireComp.clientUtils.CompetencePersonnelModel;
import fr.annuaireComp.clientUtils.ProfileModel;
import fr.annuaireComp.entities.CompetencePersonnel;
import fr.annuaireComp.entities.Fonction;
import java.util.ArrayList;
import java.util.Date;

/**
 *
 * @author MiniK
 */
@WebServlet(name = "profile", urlPatterns = {"/profile"})
public class profile extends HttpServlet {

    @EJB
    PersonnelManager persMan;

    /** 
     * Handles the HTTP <code>GET</code> method.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {

        try {


            if (!AuthManager.logUser(request, response)) {
                return;
            }

            Integer persId = Integer.parseInt(request.getParameter("id"));

            Personnel pers = persMan.getPersonnel(persId);
            if (pers == null) {
                response.sendError(404);
            } else {

                ProfileViewModel profileViewData = new ProfileViewModel();
                profileViewData.profile = ProfileModel.buildProfileModel(pers);
                profileViewData.compPersList = new ArrayList<CompetencePersonnelModel>();
                for (CompetencePersonnel cp : pers.getCompetencePersonnelList()) {
                    profileViewData.compPersList.add(new CompetencePersonnelModel(cp.getCommentaire(), cp.getAnneesXp().toString(),
                            CompetenceModel.buildCompetenceModel(cp.getCompetence(),false)));
                }

                request.setAttribute("profiledata", profileViewData);
                request.getRequestDispatcher("/WEB-INF/profile.jsp").forward(request, response);

            }
        } finally {
        }


    }

    /** 
     * Handles the HTTP <code>POST</code> method.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
    
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {

        String action = request.getParameter("action").trim();
        String id = request.getParameter("id").trim();
        String profileAttribute = request.getParameter("element_id").trim();
        String update_value = request.getParameter("update_value").trim();
        String original_html = request.getParameter("original_html").trim();
        String returnedValue = update_value;
        //   System.out.println("id: "+id + " profileAttribute: "+profileAttribute + "update_value: "+update_value);

        if ("edit".equalsIgnoreCase(action)) {
            try {
                Integer persId;

                persId = Integer.parseInt(id);

                Personnel pers = persMan.getPersonnel(persId);
                if ("datenaissance".equals(profileAttribute)) {
                    Date date = new SimpleDateFormat("dd/MM/yyyy").parse(update_value);
                    pers.setDateNaissance(date);
                }

                if ("nom".equals(profileAttribute)) {
                    pers.setNom(update_value);
                }

                if ("prenom".equals(profileAttribute)) {
                    pers.setPrenom(update_value);
                  //  pers.setEmail(id);
                }

                if ("sexe".equals(profileAttribute)) {
                    boolean sexe = false;
                    if (update_value.equals("F")) {
                        sexe = true;
                        returnedValue = "Femme";
                    } else {
                        returnedValue = "Homme";
                    }
                    pers.setSexe(sexe);

                }

                if ("bureau".equals(profileAttribute)) {
                    pers.setBureau(update_value);
                }

                if ("fonction".equals(profileAttribute)) {
                    //TODO: returner le bon label
                    Integer fonctionId = Integer.parseInt(update_value);
                    Fonction f = new Fonction(fonctionId);
                    pers.setIdfonction(f);
                    returnedValue = Consts.FONCTIONS_LABELS[fonctionId];
                }

                persMan.editPersonnel(pers);

            } catch (Exception ex) {
                returnedValue = original_html;
            } finally {
                response.getWriter().println(returnedValue);
            }

        }
    }

    /** 
     * Returns a short description of the servlet.
     * @return a String containing servlet description
     */
    @Override
    public String getServletInfo() {
        return "Short description";
    }// </editor-fold>
}
